package cn.edu.scau.common;

import cn.edu.scau.bean.ResultBean;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@ControllerAdvice(basePackages = {"com.neuedu.office","com.neuedu.office.controller","com.neuedu.office.service.Impl","com.neuedu.office.dao"})
public class GlobalExceptionController {

    /**
     * 写法一返回JSON数据 RuntimeException可以修改为子类，实现更精确的异常捕获
     * @param ex
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
        @ResponseBody
        public ResultBean exceptionHandler1(RuntimeException ex){
            return new ResultBean(5005,false,ex.getMessage(),ex);
        }


//    /**
//     * 写法二返回指定页面 RuntimeException可以修改为子类，实现更精确的异常捕获
//     * @param ex
//     * @return
//     */
//    @ExceptionHandler(RuntimeException.class)
//    public String exceptionHandler2(RuntimeException ex, Model model){
//       model.addAttribute("error",ResultBean.builder().code(500).successed(false).message(ex.getMessage()).data(ex).build());
//       return "/error/exception";
//    }
}
